home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Word8Array.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  4.8 KB  |  145 lines  |  [TEXT/Moml]

  1. (* Word8Array -- as of 1994-12-21 *)
  2.  
  3. type elem   = Word8.word;
  4. type vector = Word8Vector.vector;
  5.  
  6. local 
  7.     prim_eqtype array_;
  8.     prim_val array_   : int -> array_                 = 1 "create_string";
  9.     prim_val vector_  : int -> vector                 = 1 "create_string";
  10.     prim_val sub_     : array_ -> int -> elem         = 2 "get_nth_char";
  11.     prim_val update_  : array_ -> int -> elem -> unit = 3 "set_nth_char";
  12.     prim_val length_  : array_ -> int                 = 1 "string_length";
  13.     prim_val lengthv_ : vector -> int                 = 1 "string_length";
  14.     prim_val fill_    : array_ -> int -> int -> elem -> unit 
  15.                                                       = 4 "fill_string";
  16.     prim_val blitaa_  : array_ -> int -> array_ -> int -> int -> unit 
  17.                                                       = 5 "blit_string";
  18.     prim_val blitav_  : array_ -> int -> vector -> int -> int -> unit 
  19.                                                       = 5 "blit_string";
  20.     prim_val blitva_  : vector -> int -> array_ -> int -> int -> unit 
  21.                                                       = 5 "blit_string";
  22. in 
  23.  
  24. type array = array_ ref;
  25.  
  26. val maxLen = 16777211;                  (* = (2^22-1)*4-1, with 32 bit *)
  27.  
  28. val array0 = ref (array_ 0);
  29.  
  30. fun array(n, v: elem) =
  31.     let val a = if n < 0 orelse n > maxLen then raise Size else array_ n 
  32.     in fill_ a 0 n v; ref a end;
  33.  
  34. fun tabulate(n, f : int -> elem) =
  35.   if n < 0 orelse n > maxLen then raise Size else
  36.   let val a = array_ n
  37.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  38.   in init 0; ref a end;
  39.  
  40. fun fromList (vs : elem list) =
  41.     let val n = List.length vs
  42.         val a = if n > maxLen then raise Size else array_ n 
  43.         fun init [] i = ()
  44.           | init (v::vs) i = (update_ a i v; init vs (i+1))
  45.     in init vs 0; ref a end;
  46.  
  47. fun length (ref a) = length_ a;
  48.  
  49. fun sub(ref a, i) =
  50.   if i < 0 orelse i >= length_ a then raise Subscript 
  51.   else sub_ a i;
  52.  
  53. fun update(ref a, i, v) =
  54.   if i < 0 orelse i >= length_ a then raise Subscript 
  55.   else update_ a i v;
  56.  
  57. fun extract (ref a, i, slicelen) =
  58.     let val n = case slicelen of NONE => length_ a - i | SOME n => n 
  59.         val newvec = if i<0 orelse n<0 orelse i+n > length_ a then
  60.                              raise Subscript
  61.                          else
  62.                              vector_ n 
  63.     in blitav_ a i newvec 0 n; newvec end;
  64.  
  65. fun copy {src = ref a1: array, si=i1, len, dst = ref a2: array, di=i2} =
  66.     let val n = case len of NONE => length_ a1 - i1 | SOME k => k
  67.     in
  68.         if n<0 orelse i1<0 orelse i1+n > length_ a1
  69.                orelse i2<0 orelse i2+n > length_ a2
  70.         then raise Subscript
  71.         else blitaa_ a1 i1 a2 i2 n
  72.     end
  73.  
  74. fun copyVec {src = a1: vector, si=i1, len, dst = ref a2: array, di=i2} =
  75.     let val n = case len of NONE => lengthv_ a1 - i1 | SOME k => k
  76.     in
  77.         if n<0 orelse i1<0 orelse i1+n > lengthv_ a1
  78.                orelse i2<0 orelse i2+n > length_ a2
  79.         then raise Subscript
  80.         else blitva_ a1 i1 a2 i2 n
  81.     end
  82.  
  83. fun foldl f e (ref a) = 
  84.     let val stop = length_ a
  85.         fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  86.                        else res
  87.     in lr 0 e end
  88.  
  89. fun foldr f e (ref a) =
  90.     let fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  91.                        else res
  92.     in rl (length_ a - 1) e end
  93.  
  94. fun modify f (ref a) = 
  95.     let val stop = length_ a
  96.         fun lr j = if j < stop then (update_ a j (f(sub_ a j)); lr (j+1))
  97.                    else ()
  98.     in lr 0 end
  99.  
  100. fun app f (ref a) = 
  101.     let val stop = length_ a
  102.         fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  103.                    else ()
  104.     in lr 0 end
  105.  
  106. fun sliceend (a, i, NONE) = 
  107.         if i<0 orelse i>length a then raise Subscript
  108.         else length a
  109.   | sliceend (a, i, SOME n) = 
  110.         if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  111.         else i+n;
  112.  
  113. fun foldli f e (slice as (ref a, i, _)) = 
  114.     let fun loop stop =
  115.             let fun lr j res = 
  116.                 if j < stop then lr (j+1) (f(j, sub_ a j, res))
  117.                 else res
  118.             in lr i e end
  119.     in loop (sliceend slice) end;
  120.  
  121. fun foldri f e (slice as (ref a, i, _)) = 
  122.     let fun loop start =
  123.             let fun rl j res = 
  124.                     if j >= i then rl (j-1) (f(j, sub_ a j, res))
  125.                     else res
  126.             in rl start e end;
  127.     in loop (sliceend slice - 1) end
  128.  
  129. fun modifyi f (slice as (ref a, i, _)) = 
  130.     let fun loop stop =
  131.             let fun lr j = 
  132.                 if j < stop then (update_ a j (f(j, sub_ a j)); lr (j+1))
  133.                 else ()
  134.             in lr i end
  135.     in loop (sliceend slice) end;
  136.  
  137. fun appi f (slice as (ref a, i, _)) = 
  138.     let fun loop stop = 
  139.             let fun lr j = 
  140.                     if j < stop then (f(j, sub_ a j); lr (j+1)) 
  141.                     else ()
  142.             in lr i end
  143.     in loop (sliceend slice) end;
  144. end
  145.